﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace 默写
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AWord[] words = new AWord[4];
            words[0] = new AWord("我们");
            words[1] = new AWord("我的");
            words[2] = new AWord("你们");
            words[3] = new AWord("你的");
            ACharacter[] characters = new ACharacter[2];
            characters[0] = new ACharacter("我", ACharacterRequest.写);
            characters[1] = new ACharacter("你", ACharacterRequest.认);
            ALesson alesson = new ALesson(1, ALessonType.识字);
            ABook abook = new ABook(2, ABookLevel.上册);
            characters[0].Words.Add(words[0]);
            characters[0].Words.Add(words[1]);
            characters[1].Words.Add(words[2]);
            characters[1].Words.Add(words[3]);
            alesson.Characters.Add(characters[0]);
            alesson.Characters.Add(characters[1]);
            abook.Lessons.Add(alesson);

            treeView1.Nodes.Add(abook.ToNode());
            richTextBox1.Text = abook.ToString();

            SaveFileDialog sfd = null;
            Document document = null;
            sfd = new SaveFileDialog();
            try
            {
                sfd.Title = "保存pdf文件";
                sfd.DefaultExt = "pdf";
                sfd.Filter = "pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                DialogResult result = sfd.ShowDialog();
                if (result == DialogResult.OK)
                {

                    document = new Document(PageSize.A4, 72, 72, 72, 72);
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sfd.FileName,FileMode.Create));
                    document.Open();
                    document.Add(abook.ToPDF());
                    MessageBox.Show("PDF文件已经生成.");
                }
            }
            finally
            {
                if (sfd != null) sfd.Dispose();
                if (document != null) document.Close();
            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            StripMenuItem_addchar.Enabled = StripMenuItem_addlesson.Enabled = StripMenuItem_addword.Enabled = false;
            TreeNodeExt selectednode = (TreeNodeExt)treeView1.SelectedNode;
            switch (selectednode.NodeType) { 
                case ImageIndex.Book:
                    StripMenuItem_addlesson.Enabled = true;
                    break;
                case ImageIndex.Character:
                    StripMenuItem_addword.Enabled = true;
                    break;
                case ImageIndex.Lesson:
                    StripMenuItem_addchar.Enabled = true;
                    break;
                case ImageIndex.Root:
                    break;
                case ImageIndex.Word:
                    break;
                default:
                    break;
            }
        }

        private void StripMenuItem_addbook_Click(object sender, EventArgs e)
        {
            NewBookFrm newbookfrm = new NewBookFrm();
            if (newbookfrm.ShowDialog() == DialogResult.OK) {
                ABook abook = new ABook(newbookfrm.Grade, newbookfrm.level);
                int numfind = treeView1.Nodes.Find(abook.Grade.ToString() + ":" + abook.Level.ToString(), false).Length;
                if (numfind > 0) 
                {
                    MessageBox.Show("此课本已经存在.");
                }
                else
                {
                    treeView1.Nodes.Add(abook.ToNode());
                }
            }
        }

        private void StripMenuItem_addlesson_Click(object sender, EventArgs e)
        {
            NewLessonFrm newlessonfrm = new NewLessonFrm();
            if (newlessonfrm.ShowDialog() == DialogResult.OK)
            {
                ALesson alesson = new ALesson(newlessonfrm.LessonNumber, newlessonfrm.LessonType);
                int numfind = treeView1.SelectedNode.Nodes.Find(alesson.Type.ToString() + ":" + alesson.Number.ToString(), false).Length;
                if (numfind > 0)
                {
                    MessageBox.Show("本课已经存在，如需在本课新增生字，请在此课上点击新增生字.");
                }
                else 
                {
                    string[] characterarray = newlessonfrm.Characters.Split('\n');
                    foreach (string acharacter in characterarray)
                    {
                        string[] strarray = acharacter.Replace("：", ":").Replace(" ", "").Split(':');
                        ACharacter achar = ACharacter.FromString(strarray);
                        if (achar != null)
                        {
                            alesson.Characters.Add(achar);
                        }
                    }
                    treeView1.SelectedNode.Nodes.Add(alesson.ToNode());
                    treeView1.SelectedNode.Expand();

                }

            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定要删除所选内容？", "删除确定", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {

                treeView1.SelectedNode.Remove();
            }
        }

        private void StripMenuItem_addchar_Click(object sender, EventArgs e)
        {
            NewCharsFrm newcharsfrm = new NewCharsFrm();
            if (newcharsfrm.ShowDialog() == DialogResult.OK) {
                string[] characterarray = newcharsfrm.Characters.Split('\n');
                foreach (string acharacter in characterarray)
                {
                    string[] strarray = acharacter.Replace("：", ":").Replace(" ", "").Split(':');
                    ACharacter achar = ACharacter.FromString(strarray);

                    if (achar != null)
                    {
                        int numfind = treeView1.SelectedNode.Nodes.Find(achar.Text + ":" + achar.Request.ToString(), false).Length;
                        if (numfind == 0)
                        {
                            treeView1.SelectedNode.Nodes.Add(achar.ToNode());
                        }
                        treeView1.SelectedNode.Expand();
                    }
                }


            }
        }

        private void StripMenuItem_addword_Click(object sender, EventArgs e)
        {
            NewWordsFrm newwordsfrm = new NewWordsFrm();
            if (newwordsfrm.ShowDialog() == DialogResult.OK) {
                string[] wordsarray = newwordsfrm.Words.Replace("；",";").Split(';','\n');

                    foreach (string astr in wordsarray) {
                        int numfind = treeView1.SelectedNode.Nodes.Find(astr, false).Length;
                        if (numfind == 0) {
                            treeView1.SelectedNode.Nodes.Add(new AWord(astr).ToNode());
                        }
                        treeView1.SelectedNode.Expand();
                    }

            }
            
        }

        private void StripMenuItem_importlessonfile_Click(object sender, EventArgs e)
        {


        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = null;
            try
            {
                ofd = new OpenFileDialog();
                ofd.Title = "载入录入课文生字文件";
                ofd.DefaultExt = "txt";
                ofd.Filter = "生字文件 (*.txt)|*.txt|All files (*.*)|*.*";
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                ofd.Multiselect = false;
                DialogResult result = ofd.ShowDialog();
                if (result == DialogResult.OK)
                {
                    string[] chararray = File.ReadAllLines(ofd.FileName,Encoding.GetEncoding("GBK"));
                    if (ValidateString.ValidateCharString(chararray))
                    {
                        foreach (string achar in chararray)
                        {
                            string[] splitchar = achar.Replace("：", ":").Split(':');
                            treeView1.SelectedNode.Nodes.Add(ACharacter.FromString(splitchar).ToNode());
                        }
                    }
                    else
                    {
                        MessageBox.Show("载入文件有错误，请查证后重新载入.");
                    }
                }
            }
            catch (NullReferenceException)
            {

            }
            catch (Exception)
            {

            }

            finally
            {
                if (ofd != null) ofd.Dispose();
            }
        }
    }
}
